﻿using LiveCharts;
using LiveCharts.Wpf;
using NewLife.Serialization;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using UIWindow.Domains;
using UIWindow.Entitys;
using UIWindow.Models;

namespace UIWindow.GlobalParameters
{
    [Attributes.Singleton]
    public class HomePageGlobal
    {
        /// <summary>
        /// 一次扫码
        /// </summary>
        public AsyncObservableCollection<ProductionDataModel> ScanCodeBeforeList { get; set; }
        /// <summary>
        /// 二三次扫码
        /// </summary>
        public AsyncObservableCollection<ProductionDataModel> ScanCodeRearList { get; set; }
        /// <summary>
        /// 前称重集合
        /// </summary>
        public AsyncObservableCollection<ProductionDataModel> BeforeWeighingList { get; set; }

        /// <summary>
        /// 后称重集合
        /// </summary>
        public AsyncObservableCollection<ProductionDataModel> AfterWeighingList { get; set; }
        /// <summary>
        /// 运行日志
        /// </summary>
        public AsyncObservableCollection<RunMessage> LogList { get; set; }
        public HomePageAttributesModel HomeProperty { get; set; }
        public volatile int CarriageCount = 1;
        public ConcurrentDictionary<int, string> BayCodeQueue { get; set; }

        public HomePageGlobal()
        {
            LogList = new AsyncObservableCollection<RunMessage>();
            ScanCodeBeforeList = new AsyncObservableCollection<ProductionDataModel>();
            ScanCodeRearList = new AsyncObservableCollection<ProductionDataModel>();
            BeforeWeighingList = new AsyncObservableCollection<ProductionDataModel>();
            AfterWeighingList = new AsyncObservableCollection<ProductionDataModel>();
            BayCodeQueue = new ConcurrentDictionary<int, string>();
            BayCodeQueue.Clear();
            HomeProperty = new HomePageAttributesModel
            {
                Total = 0,
                Yield = 0,
                NGTotal = 0
            };
            HomeProperty.ScanCodeNG = 0;
            HomeProperty.OKTotal = 0;
        }

        /// <summary>
        /// 后称数据添加显示
        /// </summary>
        /// <param name="model"></param>
        public void AfterWeighingListAdd(ProductionData model)
        {
            AfterWeighingList.Insert(0, model.ToJson().ToJsonEntity<ProductionDataModel>());
            if (AfterWeighingList.Count > 400) AfterWeighingList.RemoveAt(399);
        }

        /// <summary>
        /// 前称数据添加显示
        /// </summary>
        /// <param name="model"></param>
        public void BeforeWeighingListAdd(ProductionData model)
        {
            BeforeWeighingList.Insert(0, model.ToJson().ToJsonEntity<ProductionDataModel>());
            if (BeforeWeighingList.Count > 400) BeforeWeighingList.RemoveAt(399);
        }
    }
}
